package servlet;

import entity.Book;
import service.BookService;
import service.impl.BookServiceImpl;
import util.Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddBookServlet", value = "/AddBookServlet")
public class AddBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String bookId=request.getParameter("id");
        String bookName= request.getParameter("name");
        String author=request.getParameter("author");
        String publisher=request.getParameter("publisher");
        String price=request.getParameter("price");
        Book book=new Book(bookName,bookId,publisher,Double.parseDouble(price),author,"true", Util.getDate());
        BookService bookService=new BookServiceImpl();
        System.out.println(bookId+","+bookName+","+author+","+price+","+publisher);
        if(bookService.addBook(book)){
            response.getWriter().print("添加成功！1s后返回！");
            response.setHeader("refresh","1;url=QueryBookByPageServlet");
        }else{
            response.getWriter().print("添加失败！1s后返回！");
            response.setHeader("refresh","1;url=QueryBookByPageServlet");
        }
    }
}
